"""
Descripttion: 字符串，Byte, Hex 相互转换
Version: 1.0
Author:
Date: 2023-04-19
LastEditors:
LastEditTime:
"""

import struct
from typing import List, Tuple


# str <-> byte
def str2byte(mstr_: str):
    ret = mstr_.encode('utf-8')
    return ret


def byte2str(mbyte_: bytes):
    ret = mbyte_.decode("utf-8")
    return ret


# byte <-> hex
def byte2hex(mbyte_: bytes):
    ret = mbyte_.hex()  # 转换成 hex
    return ret


def hex2byte(mhex_: str):
    ret = bytes.fromhex(mhex_)
    return ret


# str <-> hex
def str2hex(mstr_: str):
    ret = byte2hex(str2byte(mstr_))
    return ret


def hex2str(mhex_: str):
    ret = byte2str(hex2byte(mhex_))
    return ret


def digit2str(asic_list: List[int]) -> Tuple[List[str], str]:
    hex_str_lst = [hex(x).replace('0x', '') for x in asic_list]
    hex_str = ''.join(hex_str_lst)
    ret = hex2str(hex_str)
    return hex_str_lst, ret


if __name__ == '__main__':
    mstr0 = '我们的世界由原子组成'

    mbyte = str2byte(mstr0)
    mstr = byte2str(mbyte)
    print(mstr, '\n', mbyte, '\n')

    mhex = byte2hex(mbyte)
    mbyte2 = hex2byte(mhex)
    print(mhex, '\n', mbyte2, '\n')

    mhex2 = str2hex(mstr)
    mstr2 = hex2str(mhex2)
    print(mhex2, '\n', mstr2, '\n')

    lst = [78, 111, 100, 101]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # Mesh type: CellNode
    print('===========================')
    lst = [67, 101, 108, 108, 78, 111, 100, 101]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # Mesh type: FaceCell
    print('===========================')
    lst = [70, 97, 99, 101, 67, 101, 108, 108]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')  # V/m
    lst = [86, 47, 109]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')
    lst = [65, 47, 109, 94, 50]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')  # V
    lst = [86]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')
    lst = [109]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')
    lst = [117, 109]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')
    lst = [109, 94, 45, 51, 42, 109, 111, 108]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    print('===========================')
    lst = [109, 94, 45, 50, 42, 115, 94, 45, 49, 42, 109, 111, 108]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')
    """
    print('===========================')
    lst = [0, -3, 0, 0, 1, 0, 0]
    hex_lst = [struct.pack('<l', x) for x in lst]
    print(hex_lst)
    ret = b''.join(hex_lst)
    ret = ret.decode('utf-8')
    print(f'{lst} =>\n{ret}')

    """
    # ======= 流体; Total.Cell.4
    print('===========================')
    lst = [84, 111, 116, 97, 108, 46, 67, 101, 108, 108, 46, 52]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # ======= 电化学; Location; Node
    print('===========================')
    lst = [78, 111, 100, 101]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # ======= 电化学; Suffix; xyz
    print('===========================')
    lst = [120, 121, 122]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # ======= 电化学; Type; scalar
    print('===========================')
    lst = [83, 99, 97, 108, 97, 114]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # ======= 电化学; Unit; m^-3*mol
    print('===========================')
    lst = [109, 94, 45, 51, 42, 109, 111, 108]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')

    # ======= 电化学; Value.Mapping; ET_V.Node.1
    print('===========================')
    lst = [69, 84, 95, 86, 46, 78, 111, 100, 101, 46, 49]
    hex_str, mstr3 = digit2str(lst)
    print(f'{lst} =>\n{hex_str} =>\n{mstr3}')
